package com.aaa.system.service.impl;

import com.aaa.common.entity.AclPermission;
import com.aaa.common.entity.AclRolePermission;
import com.aaa.common.util.JWTUtil;
import com.aaa.common.vo.Result;
import com.aaa.security.utils.WebUtils;
import com.aaa.system.dao.AclPermissionDao;
import com.aaa.system.dao.AclRolePermissionDao;
import com.aaa.system.service.AclPermissionService;
import com.aaa.system.service.AclRolePermissionService;
import com.aaa.system.service.AclUserService;
import com.aaa.system.vo.FenPermissionVo;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.acl.Acl;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AclPermissionServiceImpl implements AclPermissionService {

    @Autowired
    private AclPermissionDao permissionDao;

    @Autowired
    private AclRolePermissionService rolePermissionService;

    @Override
    public Result findByUserId(String userid) {
        List<AclPermission> aclPermissions = permissionDao.selectByUserid(userid);
        return new Result<List<AclPermission>>(2000,"查询成功", aclPermissions);
    }
    public Result<List<AclPermission>> findByUsername(){
    //1.得到当前用户的名称
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> map = JWTUtil.getTokenChaim(token);
        String username = (String) map.get("username");

     //2.调用dao中根据用户名查询该用户具有的菜单
        List<AclPermission> permissions = permissionDao.selectByUserName(username);

        //3.方法的递归
        //1.查询一级菜单
        List<AclPermission> first=new ArrayList<>();
        for (AclPermission permission : permissions) {
            if(permission.getPid().equals("1")){
                first.add(permission);
            }
        }

        //2.查询一级菜单下的子菜单
        for (AclPermission f:first){
            f.setChildren(findChildren(permissions,f.getId()));
        }
        return new Result<>(2000,"查询左侧菜单成功",first);
    }

    @Override
    public Result<Map<String, Object>> findALl(String roleId) {
        List<AclPermission> permissions = permissionDao.selectList(null);
        //1.得到一级菜单
        List<AclPermission> first=new ArrayList<>();
        for (AclPermission permission : permissions) {
            if(permission.getPid().equals("1")){
                first.add(permission);
            }
        }
        //2.设置一级菜单的相应子菜单
        for (AclPermission p : first) {
            p.setChildren(findChildren(permissions,p.getId()));
        }
        //获取当前角色具有的权限id
        List<String>perIds=permissionDao.selectByRoleId(roleId);


        Map<String,Object>map=new HashMap<>();
        map.put("allPermissions",first);
        map.put("checkItems",perIds);

        return new Result<>(2000,"查询成功",map);
    }

    @Override
    @Transactional
    public Result confirmFenPermission(FenPermissionVo fenPermissionVo) {
        //根据角色id删除原来的具有的权限id
        UpdateWrapper<AclRolePermission> wrapper=new UpdateWrapper<>();
        wrapper.eq("role_id",fenPermissionVo.getRoleId());
        rolePermissionService.remove(wrapper);

        //批量添加角色具有的权限
        List<AclRolePermission> list=new ArrayList<>();
        for (String permissionId : fenPermissionVo.getPermissionIds()) {
            AclRolePermission aclRolePermission = new AclRolePermission();
            aclRolePermission.setIsDeleted(0);
            aclRolePermission.setGmtCreate(LocalDateTime.now());
            aclRolePermission.setGmtModified(LocalDateTime.now());
            aclRolePermission.setPermissionId(permissionId);
            aclRolePermission.setRoleId(fenPermissionVo.getRoleId());
            list.add(aclRolePermission);
        }
        rolePermissionService.saveBatch(list);


        return new Result(2000,"分配权限成功");
    }

    private List<AclPermission> findChildren(List<AclPermission> permissions,String id){
        List<AclPermission> senconds=new ArrayList<>();
        for (AclPermission p:permissions){
            if(p.getPid().equals(id)){
                senconds.add(p);
            }
        }
        for (AclPermission p:senconds){
            p.setChildren(findChildren(permissions,p.getId()));
        }
        return senconds;
    }
}
